package com.woniuxy.driverschool.service.impl;/* *
@author author
@create 2021-09-29 20:09
*/

import com.woniuxy.driverschool.entity.Period;
import com.woniuxy.driverschool.entity.PeriodDetail;
import com.woniuxy.driverschool.mapper.PeriodDetailMapper;
import com.woniuxy.driverschool.mapper.PeriodMapper;
import com.woniuxy.driverschool.service.PeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PeriodServiceImpl implements PeriodService {

    @Autowired
    PeriodMapper periodMapper;

    @Autowired
    PeriodDetailMapper periodDetailMapper;

    @Override
    public void addPeriod(Period period) {

        periodMapper.insertPeriod(period);
    }

    @Override
    public void modifyPeriod(Period period) {

        periodMapper.updatePeriod(period);
        periodDetailMapper.deletePeriodDetail(period.getPeriodId());
        periodDetailMapper.insertPeriodDetails(period.getPeriodDetails());

    }

    @Override
    public List<Period> getAllPeriods() {
        return periodMapper.selectAllPeriod();
    }

    @Override
    public Period getPeriodById(int periodId) {

        Period period = periodMapper.selectPeriodById(periodId);
        List<PeriodDetail> periodDetails = periodDetailMapper.selectPeriodDetailsByPeriodId(periodId);
        for (PeriodDetail periodDetail:periodDetails) {
            List<Date> times = periodDetail.getTimes();
            times.add(periodDetail.getTimeFrom());
            times.add(periodDetail.getTimeTo());
        }
        period.setPeriodDetails(periodDetails);
        return period;
    }

    @Override
    public void removePeriodById(int periodId) {
        periodMapper.deletePeriod(periodId);
    }
}
